package com.zja.service.Impl;

import com.zja.dao.Impl.LikesDaoImpl;
import com.zja.dao.LikesDao;
import com.zja.model.Likes;
import com.zja.service.LikesService;
import java.sql.Timestamp;

/**
 * @author JA
 * @date: 2025/5/5
 * description:
 */
public class LikesServiceImpl implements LikesService {
    private final LikesDao likesDao = new LikesDaoImpl();

    @Override
    public boolean likeArticle(Long userId, Long articleId) {
        // 判断是否已经点赞
        if (likesDao.hasUserLiked(userId, articleId)) {
            // 用户已点赞，返回 false 表示不能重复点赞
            return false;
        }

        // 否则插入新的点赞记录
        Likes like = new Likes(null, userId, articleId, new Timestamp(System.currentTimeMillis()));
        return likesDao.insert(like);
    }

    @Override
    public boolean unlikeArticle(Long userId, Long articleId) {
        // 判断是否存在该点赞记录
        if (!likesDao.hasUserLiked(userId, articleId)) {
            // 用户未点赞，无法取消
            return false;
        }

        return likesDao.deleteByUserAndArticle(userId, articleId);
    }

    @Override
    public int getLikeCount(Long articleId) {
        // 获取文章的点赞数
        return likesDao.countLikes(articleId);
    }

}
